#include <STC89C5xRC.H>
#include "delay.h"


sbit XPT_2046_CS=P3^5;
sbit XPT_2046_DCLK=P3^6;
sbit XPT_2046_DIN=P3^4;
sbit XPT_2046_DOUT=P3^7;


unsigned int xpt2046_readad(unsigned char command)
{
	unsigned char i;
	unsigned int advalue=0;
	
	XPT_2046_DCLK=0;
	XPT_2046_CS=0;
	for(i=0;i<8;i++)
	{
		XPT_2046_DIN=command&(0x80>>i);
		XPT_2046_DCLK=1;
		XPT_2046_DCLK=0;
	}
	for(i=0;i<16;i++)
	{
		XPT_2046_DCLK=1;
		Delayms(1);
		XPT_2046_DCLK=0;
		if(XPT_2046_DOUT){advalue|=(0x8000>>i);Delayms(1);}
	}
	XPT_2046_CS=1;
	if(command&0x08)
	{
		return (advalue>>8);
	}
	else
	{
		return (advalue>>4);
	}
}


